package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.base.utils.L
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.UploadFileResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.NewContactProtocol
import java.io.File

class NewContactPresenter(context: Context, mView: NewContactProtocol.IView): BasePresenter(context), NewContactProtocol.IPresenter {
    private var mView: NewContactProtocol.IView? = mView
    var addFamilyIsBusying = false
    var editFamilyIsBusying = false
    var delFamilyIsBusying = false
    private var uploadHeadImgIsBusying = false

    override fun addFamily(
        mobile: String,
        avatar: String,
        firstName: String,
        lastName: String,
        remark: String
    ) {
        if (addFamilyIsBusying) return
        addFamilyIsBusying = true
        OperationManager.getInstancce().addFamily(mobile, avatar, firstName, lastName, remark, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                addFamilyIsBusying = false
                if (state == 200) {
                    mView?.addFamilySuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                addFamilyIsBusying = false
                mView?.addFamilyFailed(code, message)
            }

        })
    }

    override fun editFamily(
        familyId: Int,
        mobile: String,
        avatar: String,
        firstName: String,
        lastName: String,
        remark: String
    ) {
        if (editFamilyIsBusying) return
        editFamilyIsBusying = true
        OperationManager.getInstancce().editFamily(familyId, mobile, avatar, firstName, lastName, remark, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                editFamilyIsBusying = false
                if (state == 200) {
                    mView?.editFamilySuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                editFamilyIsBusying = false
                mView?.editFamilyFailed(code, message)
            }

        })
    }

    override fun delFamily(familyId: Int) {
        if (delFamilyIsBusying) return
        delFamilyIsBusying = true
        OperationManager.getInstancce().delFamily(familyId, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                delFamilyIsBusying = false
                if (state == 200) {
                    mView?.delFamilySuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                delFamilyIsBusying = false
                mView?.delFamilyFailed(code, message)
            }

        })
    }

    override fun uploadHeadImg(file: File) {
        if (uploadHeadImgIsBusying) return
        uploadHeadImgIsBusying = true
        OperationManager.getInstancce().uploadHeadImg(file, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                uploadHeadImgIsBusying = false
                if (state == 200) {
                    val data = obj as ArrayList<UploadFileResponse>
                    if (data.size == 1) {
                        mView?.uploadHeadImgSuccessed(data[0].url)
                        L.d("TAG", "onSuccess: ${data[0].url}")
                    }
                }
            }

            override fun onFail(code: Int, message: String) {
                uploadHeadImgIsBusying = false
                mView?.uploadHeadImgFailed(code, message)
            }

        })
    }

}